package com.qianbao.qianbaobusiness.ui.cashier;

import android.util.Log;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.AndroidUtils;
import com.androidbase.utils.RxUtils;
import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.BossApi;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.DESedeEncrypt;
import com.google.gson.Gson;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.EmsStatus;
import com.qianbao.qianbaobusiness.json.RequestBean;
import com.qianbao.qianbaobusiness.json.SettlementDetailBean;

import java.util.HashMap;

import javax.inject.Inject;

public class SummaryBalancePresenter extends RxMvpPresenter<SummaryBalanceView> {
    BossApi bossApi;
    MyApi myApi;

    @Inject
    public SummaryBalancePresenter(MyApi myApi, BossApi api) {
        bossApi = api;
        this.myApi = myApi;
    }

    /**
     * 根据时间段请求历史账单
     *
     * @param posMerchantNo
     * @param defaultPageNum
     * @param defaultPageSize
     * @param mStartDate
     * @param mEndDate
     */
    public void queryColtCheckedPage(String posMerchantNo, int defaultPageNum, int defaultPageSize, String mStartDate, String mEndDate) {
        {
            RequestBean requestBean = new RequestBean(posMerchantNo, defaultPageNum, defaultPageSize, mStartDate, mEndDate);
            String desedeStr = "";
            try {
                desedeStr = DESedeEncrypt.encrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", new Gson()
                        .toJson(requestBean));
            } catch (Exception e) {
                e.printStackTrace();
            }
            HashMap<String, String> paraMap = new HashMap<>();
            paraMap.put("context", desedeStr);
            bossApi.queryColtCheckedPage(paraMap)
                    .compose(RxUtils.applySchedulersLifeCycle(getView()))
                    .subscribe(new RxObserver<Data<String>>() {
                        @Override
                        public void onNext(Data<String> stringData) {
                            if(RxObserver.checkJsonCode(stringData,false)){
                                try {
                                    String result = DESedeEncrypt
                                            .decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I"
                                                    , stringData.getResult().toString());
                                    Log.e("caowei========" , result);
                                    SettlementDetailBean bean = new Gson().fromJson(result, SettlementDetailBean.class);
                                    getView().onSuccess(bean);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().onError(e);
                        }
                    });
        }
    }

    /**
     * 发送历史账单
     *
     * @param historyType
     * @param date
     * @param date1
     * @param merchantNos
     * @param userNo
     * @param email
     */
    public void sendEmail(String historyType, String date, String date1,
                          String merchantNos, String userNo, String email) {
        myApi.sendEmail(historyType,date,date1,merchantNos,userNo,email)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<EmsStatus>>(){
                    @Override
                    public void onComplete() {
//                        getView().onComplete();
                    }

                    @Override
                    public void onNext(Data<EmsStatus> bean) {
                        if(RxObserver.checkJsonCode(bean,false)){
                            ToastUtils.showLongToast(AndroidUtils.getContext()
                                    .getResources().getString(R.string.send_success));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
//                        getView().error();
                    }
                });
    }
}
